package com.company;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Login
{
    static List<UserCredentials> users = new ArrayList<UserCredentials>();

    public boolean isLoggedIn = false;

    SelectOp select = new SelectOp();


    Login() {
        users.add(new UserCredentials("Librarian", "lib@gmail.com", "8882346318", "987-9876", "poiu", true));
        users.add(new UserCredentials("Apoorva", "apoorvaga29@gmail.com", "9035202808", "123-1234", "qwer", false));
        users.add(new UserCredentials("Athmeeya", "athmeeya@gmail.com", "8065202803", "123-2345", "asdf", false));
        users.add(new UserCredentials("thrupthi", "thrupthi@gmail.com", "9076438976", "123-9876", "zxcv", false));

    }


    public void userLogin()
    {
        while (true)
        {
            if (!isLoggedIn)
            {
                System.out.println("Enter your library number ");
                Scanner in = new Scanner(System.in);
                String libraryNo = in.nextLine();
                System.out.println("Enter your password ");
                String password = in.nextLine();

                checkLogin(libraryNo,password);
            }
        }

    }

    public String getLibraryId(String libraryId)
    {
        return libraryId;
    }

    private void checkLogin(String libraryNo, String password)
    {

        if (loginStatus(libraryNo, password))
        {
            System.out.println("You have successfully Logged In");
            isLoggedIn = true;
            showAppropriateMenu(libraryNo);
            logout();
        }
        else
        {
            System.out.println("Library number or Password is incorrect");
        }
    }

    public boolean showAppropriateMenu(String libraryNo) {
        if (checkIfLibrarian(libraryNo)) {
            new Librarian().librarianMenu();
            return true;
        } else {
            select.selectOperation(libraryNo);
            return false;
        }
    }

    public boolean loginStatus(String libraryNo, String password) {
        for (UserCredentials user : users) {
            if ((libraryNo.equals(user.libraryNo)) && (password.equals(user.password))) {
                return true;
            }
        }
        return false;
    }

    public boolean checkIfLibrarian(String libraryNo) {
        for (UserCredentials user : users) {
            if ((libraryNo.equals(user.libraryNo)) && (user.librarian)) {
                return true;
            }
        }
        return false;
    }



    public void displayCredentials(String libraryNo)
    {
        for (UserCredentials user : users) {
            if (libraryNo.equals(user.libraryNo)) {
                System.out.println("Name : " + user.name);
                System.out.println("Email : " + user.email);
                System.out.println("Pone NO :" + user.phNo);
                break;
            }
        }
    }

    public static String getUser(String libraryNo) {
        for (UserCredentials user : users) {
            if (libraryNo.equals(user.libraryNo)) {
                return user.name;
            }
        }
        return null;
    }

    public void logout() {
        isLoggedIn = false;
        System.out.println("You have logged out successfully");
    }

}
